<?php

/**
 * VesController short summary.
 *
 * VesController description.
 *
 * @version 1.0
 * @author TJF33
 */
namespace api\modules\v1\controllers;
use Yii;
use common\service\vesrpc\ves;
use common\models\Balance;
use common\models\BalanceType;
use common\service\blockchain\eth;

class VesController extends CoreController
{
	private $default_market = 'UPHUSDT';
	/**
	 * 发布限价订单
	 */
	public function actionOrderPutLimit()
	{
		extract($this->request);
		$market = $this->request('market',$this->default_market);
		$taker_fee_rate = 0;
		$maker_fee_rate = 0;
		$source = '';
		$pride = BalanceType::getOne(['type' => 2], ['price'])['price'];//UPH价格

		$ves_balance = ves::balance_query($this->_member['member_id']);
		if($ves_balance['error'] != null)
		    $this->error($ves_balance['error']['message']);

		try
		{
			$transaction = \Yii::$app->db->beginTransaction();
			if($side == 1)
			{
				$temp_amount = $amount - $ves_balance['result']['UPH']['available'];
				if($temp_amount > 0)
				{
					$res = Balance::transferBalanceToVes($this->_member['member_id'],2,$temp_amount,23,0,'交易所出售UPH');
					if(!$res['state'])
						throw new \Exception($res['msg']);
					$id = Yii::$app->db->getLastInsertId();
					$data = ves::balance_update($this->_member['member_id'],'UPH','SELLUPH',$id,$amount);
				}
			}
			else if($side == 2)
			{
				$temp_amount = $amount * $pride - $ves_balance['result']['USDT']['available'];
				if($temp_amount > 0)
				{
					$res = Balance::transferBalanceToVes($this->_member['member_id'],1,$temp_amount,24,0,'交易所购买UPH');
					if(!$res['state'])
						throw new \Exception($res['msg']);
					$id = Yii::$app->db->getLastInsertId();
					$data = ves::balance_update($this->_member['member_id'],'USDT','BUYUPH',$id,$amount * $pride);
				}
			}
			if($data['error'] != null)
				throw new \Exception($data['error']['message']);
			$transaction->commit();
		}
		catch (\Exception $exception)
		{
			$transaction->rollback();
            $this->error($exception->getMessage().'('.$exception->getLine().')');
		}

		$data = ves::order_put_limit($this->_member['member_id'],$market,$side,$amount,$pride,$taker_fee_rate,$maker_fee_rate,$source);
		if($data['error'] != null)
			$this->error($data['error']['message']);
		$this->out('限价订单发布',$data);
	}

	/**
	 * 发布市价订单
	 */
	public function actionOrderPutMarket()
	{
		extract($this->request);
		$market = $this->request('market',$this->default_market);
		$taker_fee_rate = 0;
		$source = '';
		$arr_pride = ves::market_last($this->request('market'));
		if($arr_pride['error'] != null)
		    $this->error($arr_pride['error']['message']);
		$pride = $arr_pride['result'];

		$ves_balance = ves::balance_query($this->_member['member_id']);
		if($ves_balance['error'] != null)
		    $this->error($ves_balance['error']['message']);

		try
		{
			$transaction = \Yii::$app->db->beginTransaction();
			if($side == 1)
			{
				$temp_amount = $amount - $ves_balance['result']['UPH']['available'];
				if($temp_amount > 0)
				{
					$res = Balance::transferBalanceToVes($this->_member['member_id'],2,$temp_amount,23,0,'交易所出售UPH');
					if(!$res['state'])
						throw new \Exception($res['msg']);
					$id = Yii::$app->db->getLastInsertId();
					$data = ves::balance_update($this->_member['member_id'],'UPH','SELLUPH',$id,$amount);
				}
			}
			else if($side == 2)
			{
				$temp_amount = $amount * $pride - $ves_balance['result']['USDT']['available'];
				if($temp_amount > 0)
				{
					$res = Balance::transferBalanceToVes($this->_member['member_id'],1,$amount * $pride,24,0,'交易所购买UPH');
					if(!$res['state'])
						throw new \Exception($res['msg']);
					$id = Yii::$app->db->getLastInsertId();
					$data = ves::balance_update($this->_member['member_id'],'USDT','BUYUPH',$id,$temp_amount);
				}
			}
			if($data['error'] != null)
				throw new \Exception($data['error']['message']);
			$transaction->commit();
		}
		catch (\Exception $exception)
		{
			$transaction->rollback();
            $this->error($exception->getMessage());
		}
		$data = ves::order_put_market($this->_member['member_id'],$market,$side,$amount,$taker_fee_rate,$source);
		if($data['error'] != null)
			$this->error($data['error']['message']);
		$this->out('市价订单发布',$data);
	}

	/**
	 * 取消订单
	 */
	public function actionOrderCancel()
	{
		$user_id = $this->_member['member_id'];
		$market = $this->request('market',$this->default_market);
		$order_id = $this->request('order_id');
		$data = ves::order_cancel($user_id,$market,$order_id);
		if($data['error'] != null)
		    $this->error($data['error']['message']);
		else
			$this->out('市场最后价格',$data);
	}

	/**
	 * 市场最后交易价格
	 */
	public function actionMarketLast()
	{
		$market = $this->request('market',$this->default_market);
		$data = ves::market_last($market);
		if($data['error'] != null)
		    $this->error($data['error']['message']);
		else
			$this->out('市场最后价格',$data);
	}

	/**
	 * 市场状态
	 */
	public function actionMarketStatus()
	{
		$market = $this->request('market',$this->default_market);
		$period = $this->request('period',86400);
		$data = ves::market_status($market,$period);
		if($data['error'] != null)
		    $this->error($data['error']['message']);
		else
			$this->out('市场状态',$data);
	}

	/**
	 * 今日市场状态
	 */
	public function actionMarketStatusToday()
	{
		$market = $this->request('market',$this->default_market);
		$data = ves::market_status_today($market);
		if($data['error'] != null)
		    $this->error($data['error']['message']);
		else
			$this->out('市场状态',$data);
	}

	/**
	 * 市场K线
	 */
	public function actionMarketKline()
	{
		$market = $this->request('market',$this->default_market);
		$start = $this->request('start');
		$end = $this->request('end');
		$interval = $this->request('interval');
		$data = ves::market_kline($market,$start,$end,$interval);
		if($data['error'] != null)
		    $this->error($data['error']['message']);
		else
			$this->out('市场K线',$data);
	}

	/**
	 * 市场深度
	 */
	public function actionOrderDepth()
	{
		$market = $this->request('market',$this->default_market);
		$limit = $this->request('limit',50);
		$interval = $this->request('interval',1);
		$data = ves::order_depth($market,$limit,$interval);
		if($data['error'] != null)
		    $this->error($data['error']['message']);
		else
			$this->out('市场深度',$data);
	}

	/**
	 * 订单列表
	 */
	public function actionOrderBook()
	{
		$market = $this->request('market',$this->default_market);
		$side = $this->request('side',1);
		$page  = $this->request('page',1);
		$limit = $this->request('page_size',10);
		$offset = ($page - 1) * $limit;

		$data = ves::order_book($market,$side,$offset,$limit);
		if($data['error'] != null)
		    $this->error($data['error']['message']);
		else
			$this->out('市场价格',$data);
	}

	/**
	 * 用户进行中的订单
	 */
	public function actionOrderPending()
	{
		$market = $this->request('market',$this->default_market);
		$page  = $this->request('page',1);
		$limit = $this->request('page_size',10);
		$offset = ($page - 1) * $limit;

		$data = ves::order_pending($this->_member['member_id'],$market,$offset,$limit);
		if($data['error'] != null)
		    $this->error($data['error']['message']);
		else
			$this->out('市场价格',$data);
	}

	/**
	 * 用户已完成的订单
	 */
	public function actionOrderFinished()
	{
		$user_id = $this->_member['member_id'];
		$market = $this->request('market',$this->default_market);
		$side = $this->request('side',0);
		$page  = $this->request('page',1);
		$limit = $this->request('page_size',10);
		$offset = ($page - 1) * $limit;
		$start_time = $this->request('start_time',0);
		$end_time = $this->request('end_time',0);

		$data = ves::order_finished($user_id,$market,$side,$offset,$limit,$start_time,$end_time);
		if($data['error'] != null)
		    $this->error($data['error']['message']);
		else
			$this->out('市场价格',$data);
	}



	public function actionBalanceQuery()
	{
		$data = ves::balance_query($this->_member['member_id']);
		if($data['error'] != null)
		    $this->error($data['error']['message']);
		else
			$this->out('用户交易所资产',$data);
	}

	public function actionBalanceUpdate()
	{
		extract($this->request);
		$data = ves::balance_update($this->_member['member_id'],$asset,$business,$business_id,$change,$detail);
		if($data['error'] != null)
		    $this->error($data['error']['message']);
		else
			$this->out('用户交易所资产更新',$data);
	}

	public function actionTest()
	{
		$eth = new eth();
		//$data = $eth->get_new_block_number();
		$data['eth'] = $eth->get_balance('0x53b72eac7675836a8c8fa75bcf5efaf648320a76');
		$data['usdt'] = $eth->get_token_balance('0x53b72eac7675836a8c8fa75bcf5efaf648320a76','USDT');
		$this->out('测试',$data);
	}
}